﻿using System;
using System.CodeDom;

namespace Wicresoft.EKA.NRemedy.CodeGenerate
{
    public class GenerateSelectionField : IGenerateField
    {
        public void Create(CodeTypeDeclaration classType, string fieldName, string fieldType, string propertyName, GenerateFieldDelegate fieldDelegate)
        {
            if (classType == null) throw new ArgumentNullException("classType");
            if (string.IsNullOrEmpty(fieldName)) throw new ArgumentNullException("fieldName");
            if (string.IsNullOrEmpty(fieldType)) throw new ArgumentNullException("fieldType");
            if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
            if (fieldDelegate == null) throw new ArgumentNullException("fieldDelegate");

            if (fieldDelegate(propertyName))
            {
                GenerateNameResolver gnr = new GenerateNameResolver();
                string _fieldName = fieldName;
                string _typeName = gnr.SelectionTypeResolver(propertyName);
                CodeMemberField _field = new CodeMemberField();
                _field.Name = _fieldName;
                _field.Type = new CodeTypeReference(_typeName);
                _field.Attributes = MemberAttributes.Private | MemberAttributes.Final;

                classType.Members.Add(_field);
            }
        }
    }
}
